'use strict';

/**
 * panes.js
 * this will contain a bunch of util files that we need
 * some of them are borrow(tm) from mean.js :)
 */

/**
 * Module dependencies.
 */
var _ = require('lodash'),
    glob = require('glob'),
    fs = require('fs'),
    path = require('path'),
    helper = require('../../../lib/methods');

var root = __dirname.replace('app/config/system' , ''),
    removePath = path.normalize(path.join(root , 'app' , 'client', 'web'));

 /**
  * Resolve environment configuration by extending each env configuration file,
  * and lastly merge/override that with any local repository configuration that exists
  * in local.js (borrow from mean.js)
  */

var mergeConfig = function()
{
    var conf = {};

    conf = _.extend(
            require('../opt/base'),
            require('../opt/' + process.env.NODE_ENV) || {}
    );
    return _.merge(conf, (fs.existsSync('../config/opt/local.js') && require('../opt/local.js')) || {});
};

module.exports = mergeConfig(); // so this get run once!

/**
 * Get the modules JavaScript files
 */
module.exports.getJavaScriptAssets = function(includeTests)
{
    // note here the two array get join together
    var output = helper.getGlobbedFiles(this.assets.lib.js.concat(this.assets.js), removePath);
    // To include tests
    if (includeTests) {
        output = _.union(output, this.getGlobbedFiles(this.assets.tests));
    }
    return output;
};

/**
 * Get the modules CSS files
 */
module.exports.getCSSAssets = function(app)
{
    var output = helper.getGlobbedFiles(this.assets.lib.css.concat(this.assets.css), removePath);
    return output;
};

/**
 * get passport files, this wrapper is to avoid re-init this over and over again 
 */
module.exports.getPassportFiles = function(files)
{
    return helper.getGlobbedFiles(files);
};
